<?php

/**
 * 文章管理模型
 */
require_once (HOME . '/include/base/BackendController.php');
class article extends BackendController {
	
	/**
	 * 文章模型
	 */
	private $mod_article;
	
	/**
	 * 传入进来的文章ID
	 */
	private $aid;
	function __construct() {
		parent::__construct();
		$this->article ();
	}
	function article() {
		$this->mod_article = spClass ( 'm_article' );
	} // end of construct
	public function index() {
		// TODO 初始化页面
		$article_result = $this->mod_article->spLinker ()->findAll ();
		// dump($article_result);
		
		$this->assign ( 'articleList', $article_result );
		$this->assign ( 'action', 'list' );
		$this->display ( 'article.layout.html' );
		
		// echo 'sdf';
	}
	/**
	 * 添加文章
	 *
	 * @author silenus
	 */
	public function add() {
		if (!IS_POST) {
			
			$this->assign('action', 'add');
			$this->display('article.layout.html');
		}else {
			$htmlData = '';
			
			if (! empty ( $_POST ['content'] )) {
				if (get_magic_quotes_gpc ()) {
					$htmlData = stripslashes ( $_POST ['content'] );
				} else {
					$htmlData = $_POST ['content'];
				}
			}
// 			echo $htmlData;
			//TODO
			$results = spClass ( "m_article" )->create ( array (
					'content' => $htmlData 
			) );
		}
	}
	/**
	 * 编辑文章
	 */
	public function edit() {
		if (! IS_POST) {
			if (FALSE == $this->_check_post ( array (
					'aid' 
			) )) {
				$this->error ( '提交参数有误' );
			} else
				$result = $this->mod_article->spLinker ()->find ( array (
						'article_id' => intval($this->spArgs ( 'aid' )) 
				) );
			
			$this->assign ( 'articleArr', $result );
			$this->assign ( 'action', 'edit' );
			$this->display ( 'article.layout.html' );
		} else {
			$result = $this->mod_article->update ( array (
						'article_id' => intval($this->spArgs ( 'aid' )) 
						
				) ,array(
						'title'=> htmlspecialchars($this->spArgs('post_title')),
						'content'=> htmlspecialchars($this->spArgs('content'))
						));
			if ($result == TRUE ){
				echo"编辑成功";
				$this->jump(spUrl('article','index'), '2');
			}
		}
		
		// TODO 取数据 then show
	}
	public function delete(){
		
			$result = spClass ( "m_article" )->deleteByPk ( intval($this->spArgs('aid') ));
			if($result == TRUE ){
				$this->jump(spUrl('article','index'), '2');
			}
	}
	/**
	 * 文章预览
	 */
	public function preview() {
		if (FALSE == $this->_check_post ('aid')) {
		}else{
			$result = spClass ( "m_article" )->find ( array (
					'article_id' => intval($this->spArgs('aid'))
			), "post_time DESC", 'content' );
			echo '<html><body>';
			echo htmlspecialchars_decode($result ['content'] ? $result ['content'] : "");
			echo '</body></html>';
		}
	}
}
?>